package com.jframe.cfg.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jframe.cfg.model.Fun;
import com.jframe.cfg.model.Sysmenu;
import com.jframe.cfg.service.IframeService;
import com.jframe.cfg.service.SysmenuService;
import com.jframe.sys.controller.ExtendAction;
import com.jframe.sys.controller.utils.*;
import com.jframe.sys.utils.*;
import com.jframe.sys.webui.Columns;
import com.jframe.sys.webui.Editor;
import com.jframe.sys.webui.Grid;

public class FunAction extends ExtendAction {
	
	private static String ACTION="funAction";
	private static String SERVICE="funService";
	private static Class<Fun> CLAZZ=Fun.class;
	
	public void list(){
		String tabId = this.pageInfo.getPageTabId(request);
		List<Columns> frozenColumns = new ArrayList<Columns>();//固定列
		List<Columns> commonColumns = new ArrayList<Columns>();//活动列
		frozenColumns.add(new Columns("ck","",40,""));
		commonColumns.add(new Columns("id", "编号", 100,""));
		commonColumns.add(new Columns("code", "权限代码", 100,""));
		commonColumns.add(new Columns("name","名称",120,""));
		commonColumns.add(new Columns("iconCls", "图标", 100,""));
		commonColumns.add(new Columns("iframeId","页签名称",120,
				new Editor("combobox"),this.getMenuIframeCombobox(tabId)));
		commonColumns.add(new Columns("js","javascript",150,""));
		commonColumns.add(new Columns("seq","顺序",100,""));
		commonColumns.add(new Columns("active","有效否",100,
				new Editor("combobox"),DicCache.getComboboxJson("dm_sys_active")));
		Grid grid = new Grid();
		// grid.setTitle("功能管理");
		grid.setUrl(this.getUrl(ACTION+".do?method=load"));
		String js = JsBuilder.getJsStr(frozenColumns, commonColumns, grid);
		this.request.setAttribute("js", js);
		this.forward("/jframe/cfg/list/funList.jsp");
	}
	
	public void load(){
		String tabId = this.pageInfo.getPageTabId(request);	
		this.baseLoad(SERVICE,CLAZZ," and groupId=? ","",StringUtil.parseInt(tabId));
	}
	
	public void save(){
		this.baseSave(SERVICE, CLAZZ);
	}
	
	public void delete(){
		this.baseDelete(SERVICE);
	}
	
	public void getObjById(){
		this.print(JsonUtil.obj2Json(this.getObject(SERVICE)));
	} 
	
	private String getMenuIframeCombobox(String tabId){
		IframeService iframeService = (IframeService)this.getBean("iframeService");		
			List<Map<String,Object>> listmap = iframeService.findListMap(StringUtil.parseInt(tabId));	
			return JsonUtil.list2Json(listmap);

	}
	
	private String getIframeCombobox(String tabId){
		//IframeService iframeService = (IframeService)this.getBean("iframeService");
		//获取根节点下的子节点，然后根据子节点查询出所有的
		SysmenuService sysmenuService = (SysmenuService)this.getBean("sysmenuService");
		List<Sysmenu> list = sysmenuService.findListByUpIdAndGroupId(1,StringUtil.parseInt(tabId));
		List<Map<String,Object>> maps = new ArrayList<Map<String,Object>>();
		for(Sysmenu menu:list){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("id", "root"+menu.getId());
			map.put("text", menu.getText());
			map.put("children", this.getChildrens(menu.getId()));
			maps.add(map);
		}
		return JsonUtil.list2Json(maps);
	}
	
	private List<Map<String,Object>> getChildrens(int upId){
		IframeService iframeService = (IframeService)this.getBean("iframeService");		
		return iframeService.findListMapByMenuUpId(upId);
	}
	
	public void getIframes(){
		String iframeId = this.pageInfo.getPageTabId(request);
		String json = getIframeCombobox(iframeId);
		this.print(json);
	}
}
